<?php

namespace app\common\service;

use app\common\service\Base;

class StockLog extends Base
{
	/**
	 * 获取已使用的数量
	 *
	 * @author KING
	 * @date 2020年6月2日
	 *
	 * @param integer $materialId
	 * @param integer $orderId
	 * @return integer
	 */
	public function getUseNum($materialId, $orderId) {
		if (empty($materialId) || empty($orderId)) return 0;
		// 使用的量
		$where = [
			'type' => 0,
			'material_id' => $materialId,
			'order_id' => $orderId
		];
		$useNum = db('stock_log')->where($where)->sum('operate_num');

		// 退回的量
		$where = [
			'type' => 2,
			'material_id' => $materialId,
			'order_id' => $orderId
		];
		$returnNum = db('stock_log')->where($where)->sum('operate_num');

		return $useNum-$returnNum;
	}

}